﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Threading;


namespace ComUtil
{
    public class ComHelper
    {
        public static ComHelper Instance = new ComHelper();
        public string GetAvailComPortsString()
        {
            string ret = "";

            foreach (string s in System.IO.Ports.SerialPort.GetPortNames())
            {
                ret += s;
                ret += ",";
            }
            if (ret.Length > 0)
            {
                ret = ret.Substring(0, ret.Length - 1);
            }
            return ret;
        }
        public string[] GetAvailComPorts()
        {


            return System.IO.Ports.SerialPort.GetPortNames();


        }
        public bool CheckComPorts(string comPorts, string strBtl)
        {
            int btl = 0;
            try
            {
                btl = int.Parse(strBtl);
            }
            catch
            {
                return false;
            }
            if (string.IsNullOrEmpty(comPorts))
            {
                return false;
            }
            System.IO.Ports.SerialPort port = new System.IO.Ports.SerialPort(comPorts, btl);
            try
            {
                if (port.IsOpen)
                {
                    port.Close();
                    port.Open();
                }
                else
                {
                    port.RtsEnable = true;

                    port.Open();//打开串口

                }
                return IsSupportATCommand(port);

            }
            catch (Exception ex)
            {
                //  Logger.Write(ex);

                if (ex.ToString().IndexOf("denied.") > -1)
                {
                    //   DevExpress.XtraEditors.XtraMessageBox.Show(comPorts+"设备拒绝访问，请尝试重新插拔设备后重试");
                }
                else
                {
                    // DevExpress.XtraEditors.XtraMessageBox.Show(comPorts+"启用失败，请尝试重新插拔设备后重试！错误信息:" + ex.Message.ToString());
                }
                return false;
            }
            finally
            {
                if (port.IsOpen)
                {
                    port.Close();
                }

            }

        }

        public bool RunCmd(string comPorts, string strBtl, string cmd)
        {
            int btl = 0;
            try
            {
                btl = int.Parse(strBtl);
            }
            catch
            {
                return false;
            }
            if (string.IsNullOrEmpty(comPorts))
            {
                return false;
            }
            System.IO.Ports.SerialPort port = new System.IO.Ports.SerialPort(comPorts, btl);
            try
            {
                if (port.IsOpen)
                {
                    port.Close();
                    port.Open();
                }
                else
                {
                    port.RtsEnable = true;

                    port.Open();//打开串口

                }
                port.Write(cmd+"\r");
                Thread.Sleep(10);
                string s = port.ReadExisting().Trim();
                if (s.IndexOf("OK") > -1)
                {
                    return true;
                }
                return false;

            }
            catch (Exception ex)
            {
                //  Logger.Write(ex);

                if (ex.ToString().IndexOf("denied.") > -1)
                {
                    //   DevExpress.XtraEditors.XtraMessageBox.Show(comPorts+"设备拒绝访问，请尝试重新插拔设备后重试");
                }
                else
                {
                    // DevExpress.XtraEditors.XtraMessageBox.Show(comPorts+"启用失败，请尝试重新插拔设备后重试！错误信息:" + ex.Message.ToString());
                }
                return false;
            }
            finally
            {
                if (port.IsOpen)
                {
                    port.Close();
                }
            }

        }

        public bool IsSupportATCommand(System.IO.Ports.SerialPort port)
        {
            bool isSucc = false;

            //更新添加连接识别
            port.Write("AT\r");
            int retryTimes = 8;
            for (int i = 0; i < retryTimes; i++)
            {
                Thread.Sleep(10);
                string s = port.ReadExisting().Trim();


                if (s.IndexOf("OK") > -1)
                {
                    Console.WriteLine("AT回馈" + s);
                    isSucc = true;
                    break;
                }
                else
                {
                    isSucc = false;
                }
            }


            if (!isSucc)
            {
                //    DevExpress.XtraEditors.XtraMessageBox.Show(port+"无法启用，请检查波特率设置是否正确!"); //硬件未连接、串口或是波特率设置错误
            }
            return isSucc;
        }
    }
}
